package com.ynet.middleground.approve.model;

import java.time.LocalDateTime;
import java.util.List;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynet.core.common.BaseReqObjPager;
import com.ynet.core.common.PagerInfo;
import com.ynet.core.exception.BusinessException;
import com.ynet.middleground.approve.bean.AddTaskAttrReq;
import com.ynet.middleground.approve.bean.QueryTaskAttrReq;
import com.ynet.middleground.approve.bean.UpdateTaskAttrReq;
import com.ynet.middleground.approve.dao.TaskAttrMapper;
import com.ynet.middleground.approve.dto.TaskAttrDTO;
import com.ynet.middleground.approve.entity.TaskAttr;
import com.ynet.middleground.approve.utils.BeanUtil;

/**
 * @author liulx
 * @description 流程任务节点属性业务处理类
 * @date 2020-06-29 14:52
 */
@Component
public class TaskAttrModel extends ServiceImpl<TaskAttrMapper, TaskAttr> implements IService<TaskAttr> {

    @Autowired
    private Mapper mapper;

    /**
     * 查询所有流程节点业务属性
     * 
     * @return 流程任务节点业务属性列表
     */
    public List<TaskAttrDTO> listTaskAttr() {
        LambdaQueryWrapper<TaskAttr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskAttr::getIsDeleted, "0");
        List<TaskAttr> list = this.list(queryWrapper);
        return BeanUtil.mapList(mapper, list, TaskAttrDTO.class);
    }

    /**
     * 分页查询审批节点业务属性
     * 
     * @param reqObjPager
     *            基础分页参数
     * @return 流程任务节点业务属性列表
     */
    public List<TaskAttrDTO> listPage(BaseReqObjPager reqObjPager) {
        PagerInfo pagerInfo = reqObjPager.getPagerInfo();
        Page<TaskAttr> page = new Page<>(pagerInfo.getPageNum(), pagerInfo.getPageSize());
        LambdaQueryWrapper<TaskAttr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskAttr::getIsDeleted, "0");
        IPage<TaskAttr> iPage = this.page(page, queryWrapper);
        pagerInfo.setTotal(iPage.getTotal());
        return BeanUtil.mapList(mapper, iPage.getRecords(), TaskAttrDTO.class);
    }

    /**
     * 根据 id 查询审批节点业务属性信息
     * 
     * @param req
     *            查询审批节点业务属性详情请求信息
     * @return 业务属性传输对象
     */
    public TaskAttrDTO getById(QueryTaskAttrReq req) {
        TaskAttr taskAttr = this.getById(req.getId());
        if (taskAttr == null || taskAttr.getIsDeleted()) {
            throw new BusinessException("审批节点业务属性不存在", "ECAP0600");
        }
        return mapper.map(taskAttr, TaskAttrDTO.class);
    }

    /**
     * 根据 id 更新业务属性信息
     * 
     * @param req
     *            修改审批节点业务属性请求信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void modifyById(UpdateTaskAttrReq req) {
        TaskAttr taskAttr = this.getById(req.getId());
        if (taskAttr == null || taskAttr.getIsDeleted()) {
            throw new BusinessException("审批节点业务属性不存在", "ECAP0600");
        }
        // 如果修改时修改了 code，要保证修改后的 code 不能重复
        if (!req.getCode().equals(taskAttr.getCode())) {
            int count = checkCountByCode(req.getCode());
            if (count > 0) {
                throw new BusinessException("该业务属性代码已存在", "ECAP0601");
            }
        }

        taskAttr = mapper.map(req, TaskAttr.class);
        taskAttr.setModifiedBy(req.getOperationUserId());
        taskAttr.setGmtModified(LocalDateTime.now());
        this.updateById(taskAttr);
    }

    /**
     * 新增审批节点业务属性
     * 
     * @param req
     *            新增审批节点业务属性请求信息
     * @return 新增后的业务属性 id
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer add(AddTaskAttrReq req) {
        TaskAttr taskAttr = mapper.map(req, TaskAttr.class);
        int count = checkCountByCode(req.getCode());
        if (count > 0) {
            throw new BusinessException("该业务属性代码已存在", "ECAP0601");
        }
        taskAttr.setCreateBy(req.getOperationUserId());
        taskAttr.setGmtCreate(LocalDateTime.now());
        taskAttr.setModifiedBy(req.getOperationUserId());
        taskAttr.setGmtModified(LocalDateTime.now());
        this.save(taskAttr);
        return taskAttr.getId();
    }

    /**
     * 根据业务属性 id 删除信息
     * 
     * @param req
     *            查询审批节点业务属性详情请求信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(QueryTaskAttrReq req) {
        TaskAttr taskAttr = this.getById(req.getId());
        if (taskAttr == null || taskAttr.getIsDeleted()) {
            throw new BusinessException("审批节点业务属性不存在", "ECAP0600");
        }
        taskAttr.setIsDeleted(true);
        this.updateById(taskAttr);
    }

    /**
     * 校验该 code 的数量
     * 
     * @param code
     *            业务属性编码
     * @return 该 code 的数量，正常 code 为 1 或 0，0 代表不存在
     */
    protected int checkCountByCode(String code) {
        LambdaQueryWrapper<TaskAttr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskAttr::getIsDeleted, "0");
        queryWrapper.eq(TaskAttr::getCode, code);
        return this.count(queryWrapper);
    }

}
